<template>
  <view class="page">
    <view class="login-bg">

      <image style="border-radius: 5px" mode="scaleToFill" src="http://soabm4v5s.hn-bkt.clouddn.com/login_bg1.png"
             @error="imageError"></image>

      <br>

      <view type="primary" class="login-bt" @click="login">
        <span style="color: white;" >微信快速登录</span>
      </view>

    </view>
     		<view class="login-btm">
          <radio style="transform:scale(0.6)" value="r1" checked="true" />
          <span style="color: #939393;font-size: 20rpx;">我已阅读并同意</span>
          <span style="color: #223a9b;font-size: 20rpx;">《台球大师助手用户协议》、《隐私协议》</span>
        </view>
  </view>
</template>

<script>
import {mapActions} from "vuex"

export default {
  data() {
    return {
      screenHeight: getApp().globalData.screenHeight,
      page: ''
    }
  },
  onLoad: function (option) {
    this.page = option.page;
  },
  methods: {
    ...mapActions(["mLogin"]),
    login() {
	  uni.showLoading({
		title: '加载中'
	  });
      let that = this
      uni.login({
        "provider": "weixin",
        "onlyAuthorize": true, // 微信登录仅请求授权认证
        success: function(event){
          const {code} = event
          let data = {
            code: code
          }
          // 使用登录凭证获取用户信息
          uni.getUserInfo({
            provider: 'weixin',
            success: function (infoRes) {
              data.avatar = infoRes.userInfo.avatarUrl
              data.username = infoRes.userInfo.nickName
              that.mLogin(data).then(res => {
                if (res.data.code === 200) {
				  uni.hideLoading();
                  that.$tip.success('登录成功!')
                  if (that.page !== ''&&that.page!==undefined) {
                    uni.navigateTo({
                      url: that.page
                    });
                  } else {

                    uni.switchTab({
                      url: '/pages/my/index'
                    });
                  }
                } else {

                  that.$tip.error('登录失败!');
                }

              })
            }
          });
        },
        fail: function (err) {
			uni.hideLoading();
          // 登录授权失败
          // err.code是错误码
        }
      })

    }
  }
}
</script>

<style>
.page {
  height: 100%;
  padding: 15rpx;
  overflow: hidden;
}

.login-bg {
  border-radius: 5rpx;
  background-color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 80%;
}

.login-pbt {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-top: 260rpx;
  background-color: #0128aa;
  border-radius: 45rpx;
  width: 80%;
  height: 90rpx;
}

.login-bt {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-top: 20rpx;
  background-color: #30ab38;
  border-radius: 45rpx;
  width: 80%;
  height: 90rpx;
}

.login-btm {
  margin-top: 20rpx;
  flex-direction: row;
  text-align: left;
  align-content: center;
}
</style>
